<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="" prop="isSee">
        <el-input
          v-model="queryParams.isSee"
          placeholder="请输入关键字 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['message:album:add']"
        >新增相册</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['message:album:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['message:album:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['message:album:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="albumList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" />
      <el-table-column label="相册说明" align="center" prop="content" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.content}}</span>
        </template>
      </el-table-column>
      <el-table-column label="查看相册" align="center" prop="">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="查看相册" placement="top">
          <el-button type="warning" size="small" icon="el-icon-folder" @click="search(scope.row)"></el-button>
        </el-tooltip>
        </template>
        </el-table-column>
      <el-table-column label="归档日期" align="center" prop="uploadTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加日期" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
           <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAddOne(scope.row)"
            v-hasPermi="['message:album:edit']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['message:album:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['message:album:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
   <!-- 图片预览   放在表格外面，这是个弹出狂-->
   <el-dialog :visible.sync="dialogVisible" :modal="false" title="图片预览" style="width: 100%; margin: 0 auto 50px;" >
        <img :src="previewpic" alt=""  width="100%"  height="100%"/>
        <span slot="footer" class="dialog-footer">
          </span>
       </el-dialog>
  <!-- 图片预览 -->
    <!-- 添加或修改会员相册对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="相册描述">
          <el-input type="text" v-model="form.content" placeholder="请输入相册描述"/>
        </el-form-item>
        <el-form-item label="上传时间" prop="uploadTime">
          <el-date-picker clearable
            v-model="form.uploadTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上传时间">
          </el-date-picker>
        </el-form-item>
         <el-form-item label="图片地址" prop="pcUrl">
          <el-upload
          ref="upload"
          :action="upload.url"
          :headers="upload.headers"
          list-type="picture-card"
          :multiple="multiple"
          :file-list="this.form.imgShowList"
          :on-preview="handlePictureCardPreview"
          :on-success="handleAvatarSuccess"
          >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
       <!-- 修改会员相册对话框 -->
       <el-dialog :title="title" :visible.sync="updateOpen" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="相册描述">
          <el-input type="text" v-model="form.content" placeholder="请输入相册描述"/>
        </el-form-item>
        <el-form-item label="上传时间" prop="uploadTime">
          <el-date-picker clearable
            v-model="form.uploadTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上传时间">
          </el-date-picker>
        </el-form-item>
         <el-form-item label="图片地址" prop="pcUrl">
          <el-row>
      <el-col :span="6" v-for="(o, index) in urlList" :key="index">
        <el-card class="box-card">
          <img :src="o" class="image"  @click="previewPic(o)">
          
        </el-card>
        <el-button size="mini" type="danger" icon="el-icon-delete" @click="delImage(form,o)" class="btn" circle></el-button>
      </el-col>
        </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelImage">关 闭</el-button>
      </div>
    </el-dialog>
    <!--相册-->
    <el-dialog :title="title" :visible.sync="sreachOpen" width="70%" append-to-body>
      <el-row>
      <el-col :span="6" v-for="(o, index) in imageUrlList" :key="index">
        <el-card class="box-card">
          <img :src="o" class="image"  @click="previewPic(o)" width="200px" height="200px">
        </el-card>
      </el-col>
    </el-row>
    </el-dialog>

     <!-- 添加单张相片对话框 -->
    <el-dialog :title="title" :visible.sync="addOne" width="50%" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-upload
        ref="upload"
        :action="upload.url"
        :headers="upload.headers"
        list-type="picture-card"
        :multiple="multiple"
        :file-list="this.form.imgShowList"
        :on-preview="handlePictureCardPreview"
        :on-success="handleAvatarSuccess"
        >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
      </el-form>
         <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUpload">确 定</el-button>
        <el-button @click="cancelUpload">取 消</el-button>
      </div>
    </el-dialog>
    <!--图片上传后预览-->
  <el-dialog :title="title" :visible.sync="imagesDialog" width="50%" append-to-body>
    <img :src="dialogImageUrl" width="100%" alt="">
  </el-dialog>
  </div>
</template>

<script>
import { listAlbum, getAlbum, delAlbum, addAlbum, updateAlbum,delImage } from "@/api/message/album";
import { getToken } from "@/utils/auth";
import { ColorStyle } from "quill";
import { updateData } from "../../../api/system/dict/data";
export default {
  name: "Album",
  data() {
    return {
      dialogVisible: false,
      previewpic: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员相册表格数据
      albumList: [],
      imgList:[],
      urlList:[],
      // 弹出层标题
      title: "",
      sreachOpen:false,
      // 是否显示弹出层
      open: false,
      //修改相册弹出层
      updateOpen:false,
      //图片数组
      imageUrlList:[],
      //是否显示添加单张图片弹出层
      addOne:false,
      imageUrl:'',
      multiple:true,
      imagesDialog:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        asId: null,
        content: null,
        pcUrl: null,
        uploadTime: null,
        status: null,
        isSee: null
      },
     

      //用来预览的图片
      dialogImageUrl:'',
       upload:{
          // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/content/press/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        asId: [
          { required: true, message: "协会id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    createIndex(index){
  return (this.queryParams.pageNum - 1) * (this.queryParams.pageSize) + index + 1;
    },
    /** 查询会员相册列表 */
    getList() {
      this.loading = true;
      listAlbum(this.queryParams).then(res => {
        console.info(res);
        this.albumList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    //上传的file数组
    fielChange(file,fileList){
      this.imgList.push(file.raw);
      this.form.imgShowList.push({name:file.name,url:file.url});
    },
    handleAvatarSuccess(res, file) {
        this.imgList.push(res.data)
        this.form.imgUrlList.push(res.data);
        console.info(this.form.imgUrlList);
      },
    //预览图片
    handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
    //打开相册
    search(row){
      console.info(row);
      this.imageUrlList=row.pcUrlList;
      this.sreachOpen=true;
      this.title="查看相册"
    },
     //预览大图
     previewPic(url) {
           this.previewpic = url;
           this.dialogVisible = true;
        },    
    //取消单张相片上传
    cancelUpload(){
      this.addOne=false;
      this.reset;
    },
    //关闭修改弹窗
    cancelImage(){
      this.updateOpen=false;
      this.reset();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        asId: null,
        content: null,
        pcUrl: null,
        uploadTime: null,
        createTime: null,
        updateTime: null,
        status: 0,
        isSee: null,
        imgUrlList:[],
        //页面展示的图片数组
        imgShowList:[],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员相册";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAlbum(id).then(res => {
        this.form = res.data;
        this.urlList=res.data.pcUrlList;
        this.updateOpen = true;
        this.title = "修改会员相册";
      });
    },
    handleAddOne(row){
      this.reset();
      this.form.id=row.id;
      this.addOne=true;
      this.title="增加相片"
    },
    //提交照片表单
    submitUpload(){
      this.$refs["form"].validate(valid=>{
        if(valid){
          updateAlbum(this.form).then(res=>{
            this.$modal.msgSuccess("修改成功");
            this.addOne=false;
            this.reset();
            this.getList();
          })
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAlbum(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlbum(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**删除图片*/
    delImage(data,val){
      console.info(data.id);
      console.info(val);
      this.$modal.confirm('是否删除该图片?').then(function(){
        return delImage(data.id,val);
      }).then(()=>{
        getAlbum(data.id).then(res => {
        this.form = res.data;
        this.urlList=res.data.pcUrlList;})
        this.$modal.msgSuccess("删除成功");
      }).catch(()=>{});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员相册编号为"' + ids + '"的数据项？').then(function() {
        return delAlbum(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('message/album/export', {
        ...this.queryParams
      }, `album_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
 .el-tooltip__popper{
    max-width:20%;
  }
  .el-tooltip__popper,.el-tooltip__popper.is-dark{
    background:rgb(27, 100, 190) !important;
    color: #fff !important;
    line-height: 20px;
    font-size: 15px;
  } 

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .box-card {
    margin: 5px 5px 0px 5px;
  }
  .image {
    width: 100%;
    display: block;
  }
  .btn{
    margin-left: auto;
    margin-right: auto;
    display: flex;
  }
</style>
